package com.cyq.dascma.handler.valida.impl;

import com.cyq.dascma.constant.ValidaEnum;
import com.cyq.dascma.entity.Course;
import com.cyq.dascma.exception.ServiceException;
import com.cyq.dascma.handler.valida.Valida;
import com.cyq.dascma.result.ResultGenerator;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 *
 * dingwen
 * 2021/4/4 17:23
 **/
public class CourseValidaImpl implements Valida<Course> {
    @Override
    public void valida(ValidaEnum validaEnum, Course course) {
        if (ObjectUtils.isEmpty(course)) {
            throw new ServiceException(ResultGenerator.genFailureResult("course不能为空").setCode(400));
        }
        switch (validaEnum) {
            case ADD:
                if (!StringUtils.hasText(course.getCourseId())) {
                    throw new ServiceException(ResultGenerator.genFailureResult("课程ID不能为空").setCode(400));
                }
                if (!StringUtils.hasText(course.getTeacherId())) {
                    throw new ServiceException(ResultGenerator.genFailureResult("授课教师ID不能为空").setCode(400));
                }
                if (!StringUtils.hasText(course.getCourseName())) {
                    throw new ServiceException(ResultGenerator.genFailureResult("课程名称不能为空").setCode(400));
                }
                if (!StringUtils.hasText(course.getCourseType().toString())) {
                    throw new ServiceException(ResultGenerator.genFailureResult("课程类型不能为空").setCode(400));
                }
                break;
            case MODIFY:
                if (!StringUtils.hasText(course.getCourseId())) {
                    throw new ServiceException(ResultGenerator.genFailureResult("课程ID不能为空").setCode(400));
                }
                if (!StringUtils.hasText(course.getTeacherId())) {
                    throw new ServiceException(ResultGenerator.genFailureResult("授课教师ID不能为空").setCode(400));
                }
                break;
        }
    }
}
